package com.server.Service;

import com.server.Example.MoviesToTypeExample;
import com.server.entity.Movies;
import com.server.entity.MoviesToType;
import com.server.entity.MoviesType;
import com.server.mapper.MoviesToTypeMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

/**
 * The type Movies to type service.
 * Movie ticket service implementation class
 *
 * @author user
 */
public class MoviesToTypeService {
    /**
     * Select movie to type array list.
     * Get a list of movie information that can be seen
     *
     * @return the array list Calling method: MoviesToTypeService.selectMovieToType()
     */
    public static ArrayList<MoviesToType> selectMovieToType(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesToTypeMapper moviesToTypist = sqlSession.getMapper(MoviesToTypeMapper.class);
        MoviesToTypeExample newMoviesToTypeExample = new MoviesToTypeExample();
        Date date = new Date();
        newMoviesToTypeExample.createCriteria().andMoviesTypeCreateBetween(date,new Date(date.getTime() + 7 * 24 * 60 * 60 * 1000L)).andMoviesToShowEqualTo(true);
        ArrayList<MoviesToType> userList = (ArrayList<MoviesToType>) moviesToTypist.selectByExample(null);
        sqlSession.close();
        return userList;
    }

    /**
     * Instr vales movies to type int.
     * Insert randInteger record messages in the data table, and randomly generate data records in the time interval from randStarting to randEnding
     *
     * @param randInteger  the rand integer
     * @param randStarting the rand starting
     * @param randEnding   the rand ending
     * @return the int Calling method: MoviesToTypeService.instrValesMoviesToType(randInteger,randStarting,randEnding)
     */
    public static int instrValesMoviesToType(Integer randInteger, String randStarting, String randEnding) {
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesToTypeMapper moviesToTypist = sqlSession.getMapper(MoviesToTypeMapper.class);
        int sumCount = 0;
        ArrayList<Movies> moviesArray=MoviesService.nextWeekMovies();
        ArrayList<MoviesType> moviesTypes = MoviesTypeService.selectiveGenres();
        for (int i = 0; i <randInteger ; i++) {
            sumCount+=moviesToTypist.insert(new MoviesToType(moviesArray.get(new Random().nextInt(moviesArray.size())).getMovieId(),
                    moviesTypes.get(new Random().nextInt(moviesTypes.size())).getMoviesTypeId(),
                    Utils.randomDate(randStarting, randEnding),
                    new Random().nextBoolean()
            ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }

    /**
     * Select by primary key movies to type.
     * Get information about a single piece of data
     *
     * @param id the id
     * @return the movies to type Calling method: MoviesToTypeService.selectByPrimaryKey(id)
     */
    public static MoviesToType selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesToTypeMapper movies = sqlSession.getMapper(MoviesToTypeMapper.class);
        MoviesToType moviesToType =movies.selectByPrimaryKey(id);
        sqlSession.close();
        return moviesToType == null ?new MoviesToType():moviesToType;
    }
}
